﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace UE.SharePoint.ImportExport.BLL.SharePoint
{
    /// <summary>
    /// 
    /// </summary>
 	public class UserFinder : LookupFinder
	{
		public UserFinder(SPList list) : 
            base(list)
		{
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookupItem"></param>
        /// <returns></returns>
        public List<SPListItem> Find(LookupItem lookupItem)
        {
            IList<LookupItem> lookupItems = new List<LookupItem>();
            lookupItems.Add(lookupItem); 
            return Find(lookupItems);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookupItems"></param>
        /// <returns></returns>
		public override List<SPListItem> Find(IList<LookupItem> lookupItems)
		{
            foreach (var lookupItem in lookupItems)
            {
                SPUser sPUser = this.sourceList.ParentWeb.EnsureUser(lookupItem.Value);
                if (sPUser != null)
                {
                    lookupItem.Value = sPUser.ID.ToString();
                } 
            }

            SPQuery query = this.MakeQuery(lookupItems);
            query.ViewFields = "<FieldRef Name='Name' />";
            var items = this.LookupList.GetItems(query).Cast<SPListItem>().ToList();
            return items; 
		}

		protected override void InitLookup()
		{
			base.LookupList = this.sourceList.ParentWeb.SiteUserInfoList; 
		}
	}

}
